home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / NR4HDR.C < prev    next >
Text File  |  1993-08-09  |  3KB  |  145 lines

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "config.h"
  7. #ifdef NETROM
  8. #include "mbuf.h"
  9. #include "nr4.h"
  10.  
  11. /* Convert a net/rom transport header to host format structure.
  12.  * Return -1 if error, 0 if OK.
  13.  */
  14. int
  15. ntohnr4(hdr,bpp)
  16. struct nr4hdr *hdr;
  17. struct mbuf **bpp;
  18. {
  19.     unsigned char tbuf[NR4MINHDR];
  20.     int i;
  21.  
  22.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  23.         return -1;
  24.  
  25.     hdr->opcode = tbuf[4];
  26.  
  27.     switch(tbuf[4] & NR4OPCODE){
  28.         case NR4OPPID:        /* protocol ID extension */
  29.             hdr->u.pid.family = tbuf[0];
  30.             hdr->u.pid.proto = tbuf[1];
  31.             break;
  32.         case NR4OPCONRQ:    /* connect request */
  33.             hdr->u.conreq.myindex = tbuf[0];
  34.             hdr->u.conreq.myid = tbuf[1];
  35.             if((i = PULLCHAR(bpp)) == -1)
  36.                 return -1;
  37.             hdr->u.conreq.window = i;
  38.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  39.                 return -1;
  40.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  41.                 return -1;
  42.             break;
  43.         case NR4OPCONAK:    /* connect acknowledge */
  44.             hdr->yourindex = tbuf[0];
  45.             hdr->yourid = tbuf[1];
  46.             hdr->u.conack.myindex = tbuf[2];
  47.             hdr->u.conack.myid = tbuf[3];
  48.             if((i = PULLCHAR(bpp)) == -1)
  49.                 return -1;
  50.             hdr->u.conack.window = i;
  51.             break;
  52.         case NR4OPDISRQ:    /* disconnect request */
  53.             hdr->yourindex = tbuf[0];
  54.             hdr->yourid = tbuf[1];
  55.             break;
  56.         case NR4OPDISAK:    /* disconnect acknowledge */
  57.             hdr->yourindex = tbuf[0];
  58.             hdr->yourid = tbuf[1];
  59.             break;
  60.         case NR4OPINFO:        /* information frame */
  61.             hdr->yourindex = tbuf[0];
  62.             hdr->yourid = tbuf[1];
  63.             hdr->u.info.txseq = tbuf[2];
  64.             hdr->u.info.rxseq = tbuf[3];
  65.             break;
  66.         case NR4OPACK:        /* information acknowledge */
  67.             hdr->yourindex = tbuf[0];
  68.             hdr->yourid = tbuf[1];
  69.             hdr->u.ack.rxseq = tbuf[3];
  70.             break;
  71.         default:        /* what kind of frame is this? */
  72.             return -1;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. /* Convert host-format level 4 header to network format */
  78. struct mbuf *
  79. htonnr4(struct nr4hdr *hdr)
  80. {
  81.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  82.     struct mbuf *rbuf;
  83.     char *cp;
  84.     unsigned char opcode = hdr->opcode & NR4OPCODE;
  85.  
  86.     if(opcode >= NR4NUMOPS)
  87.         return NULLBUF;
  88.  
  89.     if(hdr == NULL)
  90.         return NULLBUF;
  91.  
  92.     rbuf = alloc_mbuf(hlen[opcode]);
  93.  
  94.     rbuf->cnt = hlen[opcode];
  95.     cp = rbuf->data;
  96.  
  97.     cp[4] = hdr->opcode;
  98.  
  99.     switch(opcode){
  100.         case NR4OPPID:
  101.             *cp++ = hdr->u.pid.family;
  102.             *cp = hdr->u.pid.proto;
  103.             break;
  104.         case NR4OPCONRQ:
  105.             *cp++ = hdr->u.conreq.myindex;
  106.             *cp++ = hdr->u.conreq.myid;
  107.             cp += 3; /* skip to sixth byte */
  108.             *cp++ = hdr->u.conreq.window;
  109.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  110.             cp += AXALEN;
  111.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  112.             cp += AXALEN;
  113.             break;
  114.         case NR4OPCONAK:
  115.             *cp++ = hdr->yourindex;
  116.             *cp++ = hdr->yourid;
  117.             *cp++ = hdr->u.conack.myindex;
  118.             *cp++ = hdr->u.conack.myid;
  119.             cp++;    /* already loaded pid */
  120.             *cp = hdr->u.conack.window;
  121.             break;
  122.         case NR4OPDISRQ:
  123.             *cp++ = hdr->yourindex;
  124.             *cp = hdr->yourid;
  125.             break;
  126.         case NR4OPDISAK:
  127.             *cp++ = hdr->yourindex;
  128.             *cp = hdr->yourid;
  129.             break;
  130.         case NR4OPINFO:
  131.             *cp++ = hdr->yourindex;
  132.             *cp++ = hdr->yourid;
  133.             *cp++ = hdr->u.info.txseq;
  134.             *cp = hdr->u.info.rxseq;
  135.             break;
  136.         case NR4OPACK:
  137.             *cp++ = hdr->yourindex;
  138.             *cp++ = hdr->yourid;
  139.             *++cp = hdr->u.ack.rxseq;
  140.             break;
  141.     }
  142.     return rbuf;
  143. }
  144.  
  145. #endif /* NETROM */